(function() {

	var BusCon = require("../../core/bus/_con");
	var GoodsCon = require("../../core/goods/_con");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	var ResponObj = require("../_include/responObj");

	module.exports = {
		prefix: "/goods",
		//get公共方法，无需任何使用者登录即可使用的接口
		get: {
			//获取商品信息
			"/byId/:goods_id": function() {
				var goods_id = this.params.goods_id;
				var goods = GoodsCon.get(goods_id);
				if (goods) {
					goods = goods.getDetail();
					this.body = ResponObj("json", goods);
				} else {
					this.body = ResponObj("error", "找不到指定商品")
				}
			},
			"/byIds": function() {
				var goods_id_list = this.query.goods_id_list;
				if (goods_id_list instanceof Array) {
					this.body = ResponObj("json", goods_id_list.map(function(goods_id) {
						var goods = GoodsCon.get(goods_id);
						if (!goods) {
							throwCusError("找不到指定商品");
						}
						return goods.getDetail();
					}));
				} else {
					throwCusError("参数错误")
				}
			},
			//根据查找商品
			//TODO:更复杂的搜索算法
			//.query.bus_id 商家编号
			"/search": function() {
				var query = this.query;
				var bus_id = query.bus_id;
				var key = query.q;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					throwCusError("找不到指定商家")
				}
				var result = bus.searchGoods(key, query);
				this.body = ResponObj("json", result);
			},
			"/search/with_page_info": function() {
				var query = this.query;
				var bus_id = query.bus_id;
				var key = query.q;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					throwCusError("找不到指定商家")
				}
				var result = bus.searchGoods_PageInfo(key, query.num, query.page, query);
				this.body = ResponObj("json", result);
			},
			//获取商家商品列表
			//.query.bus_id 商家编号
			//.query.num 数量，为空或者0时返回全部
			//.query.page 与上一个参数相对应，页号
			"/list": function() {
				var query = this.request.query;
				var bus_id = query.bus_id;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					throwCusError("找不到指定商家");
				}
				var goods_list = bus.getGoodsList(query.num, query.page, query);
				this.body = ResponObj("json", goods_list);
			},
			"/list/with_page_info": function() {
				var query = this.request.query;
				var bus_id = query.bus_id;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					throwCusError("找不到指定商家");
				}
				var info = bus.getGoodsList_PageInfo(query.num, query.page, query);
				this.body = ResponObj("json", info);
			},
			"/list/with_page_info_and_end_pay_cb_url": function() {
				var query = this.request.query;
				var bus_id = query.bus_id;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					throwCusError("找不到指定商家");
				}
				var info = bus.getGoodsList_PageInfo_EndPayCbUrl(query.num, query.page, query);
				this.body = ResponObj("json", info);
			},
			//获取精品商品
			//.query.bus_id 商家编号
			//.query.num 数量，为空或者0时返回全部
			//.query.page 与上一个参数相对应，页号
			"/list/quality": function() {
				var query = this.request.query;
				var bus_id = query.bus_id;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					throwCusError("找不到指定商家");
				}
				var quality_goods_list = bus.getQGoodsList(query.num, query.page, query);
				this.body = ResponObj("json", quality_goods_list);
			},
			"/list/quality/with_page_info": function() {
				// body...
			},
			//获取商品总的数量
			//.query.bus_id 商家编号
			"/list/num": function() {
				var query = this.request.query;
				var bus_id = query.bus_id;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					throwCusError("找不到指定商家");
				}
				this.body = ResponObj("json", bus.getGoodsListTotalNum(query));
			},
			//获取评论
			//.query.bus_id 商家编号
			//.query.num 数量，为空或者0时返回全部
			//.query.page 与上一个参数相对应，页号
			"/comments/:goods_id": function() {
				var query = this.request.query;
				var bus_id = query.bus_id;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					this.body = ResponObj("error", "找不到指定商家");
					return;
				}
				var goods_id = this.params.goods_id;
				var goods = GoodsCon.get(goods_id);
				if (goods) {
					var comments = goods.getCommentList(query.num, query.page);
					this.body = ResponObj("json", goods);
				} else {
					this.body = ResponObj("error", "找不到指定商品")
				}
			},
			//随机商品，无分页信息
			//query.num
			"/list/random": function() {
				var query = this.request.query;
				var bus_id = query.bus_id;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					this.body = ResponObj("error", "找不到指定商家");
					return;
				}
				this.body = ResponObj("json", bus.getRandomGoodsList(query.num, query));
			},
			"/list/with_id_list": function() {
				var query = this.request.query;
				var bus_id = query.bus_id;
				var bus = BusCon.get(bus_id);
				if (!bus) {
					this.body = ResponObj("error", "找不到指定商家");
					return;
				}
				this.body = ResponObj("json", bus.getGoodsList_IdList(query.goods_id_list, query));
			}
		},
		put: {}
	}
}());